<template>
  <div id="root">
    <div class="todo-container">
      <div class="todo-wrap">
        <MyHeader :addTodo="addTodo" />
        <MyList
          :todoList="todoList"
          :checkTodo="checkTodo"
          :deleteTodo="deleteTodo"
        />
        <MyFooter
          :todoList="todoList"
          :checkAllTodo="checkAllTodo"
          :clearAllTodo="clearAllTodo"
        />
      </div>
    </div>
  </div>
</template>

<script>
import MyHeader from "./components/MyHeader.vue";
import MyFooter from "./components/MyFooter.vue";
import MyList from "./components/MyList.vue";

export default {
  name: "App",
  components: {
    MyHeader,
    MyFooter,
    MyList,
  },
  data() {
    return {
      todoList: [
        { id: "001", title: "吃饭", done: true },
        { id: "002", title: "睡觉", done: false },
        { id: "003", title: "打豆豆", done: true },
      ],
    };
  },

  /* 
    子传父：
​	    父组件  :receive="receive函数"   methods:{receive(x)}
​	    子组件  props:["receive"] , methods:{receive(待传递数据)}
      父组件传递给子组件一个带参数的函数，子组件通过props接收传递的函数
      在methods中调用改函数，并把要传递给父组件的数据作为函数的参数返回。
   */
  methods: {
    // 添加一个todo对象
    addTodo(todoObj) {
      // 在todolist的末尾添加接收到的todoObj
      this.todoList.unshift(todoObj);
    },
    // 勾选或取消todo对象
    checkTodo(id) {
      this.todoList.forEach((todo) => {
        if (todo.id === id) {
          todo.done = !todo.done;
        }
      });
    },

    // 删除一个todo
    deleteTodo(id) {
      // filter过滤器(不改变原数组) 参数为todo项，return todo.id不为传递的id的todo项
      this.todoList = this.todoList.filter((todo) => {
        return todo.id != id;
      });
    },

    // 全选或全不选
    checkAllTodo(done) {
      this.todoList.forEach((todo) => {
        return (todo.done = done);
      });
    },

    // 清除已完成
    clearAllTodo() {
      // filter过滤器(不改变原数组) 参数为todo项，return todo.done不为ture的项
      this.todoList = this.todoList.filter((todo) => {
        // return todo.done != true;  可以简写为 ！todo.done
        return !todo.done;
      });
    },
  },
};
</script>

<style>
/*base*/
body {
  background: #fff;
}

.btn {
  display: inline-block;
  padding: 4px 12px;
  margin-bottom: 0;
  font-size: 14px;
  line-height: 20px;
  text-align: center;
  vertical-align: middle;
  cursor: pointer;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2),
    0 1px 2px rgba(0, 0, 0, 0.05);
  border-radius: 4px;
}

.btn-danger {
  color: #fff;
  background-color: #da4f49;
  border: 1px solid #bd362f;
}

.btn-danger:hover {
  color: #fff;
  background-color: #bd362f;
}

.btn:focus {
  outline: none;
}

.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>